package com.mt.system.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.system.entity.MenuEntity;
import com.mt.system.mapper.MenuEntityMapper;

@Component
public class MenuServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(MenuServiceModel.class);

    @Autowired
    private MenuEntityMapper menuEntityMapper;

    public ServiceResult<List<MenuEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.menuEntityMapper, "Property 'menuEntityMapper' is required.");
        ServiceResult<List<MenuEntity>> result = new ServiceResult<List<MenuEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }

            result.setResult(this.menuEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[SupplierImageServiceImpl][findList]：query findList occur exception", e);
        }
        return result;
    }

    public ServiceResult<MenuEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.menuEntityMapper, "Property 'menuEntityMapper' is required.");
        ServiceResult<MenuEntity> result = new ServiceResult<MenuEntity>();
        try {
            result.setResult(this.menuEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[MenuServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insert(MenuEntity menuEntity) {
        Assert.notNull(this.menuEntityMapper, "Property 'menuEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.menuEntityMapper.insert(menuEntity);
            if (save > 0) {
                id = menuEntity.getMenuId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[MenuServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(MenuEntity menuEntity) {
        Assert.notNull(this.menuEntityMapper, "Property 'menuEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.menuEntityMapper.updateByPrimaryKeySelective(menuEntity);
            if (save > 0) {
                id = menuEntity.getMenuId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[MenuServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer menuId) {
        Assert.notNull(this.menuEntityMapper, "Property 'menuEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            int save = this.menuEntityMapper.deleteByPrimaryKey(menuId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[MenuServiceModel][deleteByPrimaryKey]:query update occur exception", e);
        }
        return result;
    }
}
